package com.csii.pos.query;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.csii.adhoc.annotation.OperationLog;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.adhoc.util.StringUtils;
import com.csii.adhoc.util.excel.ExcelUtils;
import com.csii.adhoc.util.myabtisplus.QueryWrapperUtils;
import com.csii.adhoc.controller.BaseController;
import com.csii.pos.query.service.IExcelTemplateService;
import com.csii.pos.query.service.IFiBrDailyService;
import com.csii.pos.query.vo.FiBrDailyVO;
import com.csii.result.APIResult;
import com.csii.result.PageVo;
import com.csii.result.TableHtmlBuilder;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


 /**
 * @Description: 中小金融机构日报
 * @Author: power-stdio
 * @Date:   2021-07-07
 * @Version: V1.0
 */

@RestController
@RequestMapping("/fiBrDaily")
@Slf4j
public class FiBrDailyController extends BaseController<FiBrDaily, IFiBrDailyService> {
	@Autowired
	private IFiBrDailyService fiBrDailyService;
	 @Autowired
	 private IExcelTemplateService excelTemplateService;
	 @Autowired
	 private JdbcTemplate jdbcTemplate;
	/**
	 * 分页列表查询
	 */
	@OperationLog(value = "中小金融机构日报-分页列表查询")
	@PostMapping(value = "/list")
	public APIResult<?> queryPageList(@RequestBody FiBrDailyVO fiBrDailyVO) {
		FiBrDaily fiBrDaily  = ConvertUtils.convert(FiBrDaily.class,fiBrDailyVO);
        QueryWrapper<FiBrDaily> queryWrapper = QueryWrapperUtils.getEqQueryWrapperFromDto(fiBrDaily,new QueryWrapper<>());
		Page<FiBrDaily> page = new Page<FiBrDaily>(fiBrDailyVO.getPageNo(), fiBrDailyVO.getPageSize());
		queryWrapper.orderByAsc("SEQ_NO");
		IPage<FiBrDaily> pageList = fiBrDailyService.page(page, queryWrapper);
        String html = TableHtmlBuilder.buildTableHtml(pageList.getRecords());
		return  APIResult.successResult(PageVo.getPageVoFromIPage(pageList)).setTableHtml(html);
	}

	/**
	 *   添加
	 */
	@OperationLog(value = "中小金融机构日报-添加")
	@PostMapping(value = "/add")
	public APIResult<?> add(@RequestBody FiBrDailyVO fiBrDailyVO) {
		FiBrDaily fiBrDaily  = ConvertUtils.convert(FiBrDaily.class,fiBrDailyVO);

		fiBrDailyService.save(fiBrDaily);
		return APIResult.successResult("添加成功！");
	}

	/**
	 *  编辑
	 *
	 */
	@OperationLog(value = "中小金融机构日报-编辑")
	@PostMapping(value = "/edit")
	public APIResult<?> edit(@RequestBody FiBrDailyVO fiBrDailyVO) {
		FiBrDaily fiBrDaily  = ConvertUtils.convert(FiBrDaily.class,fiBrDailyVO);
		fiBrDailyService.updateById(fiBrDaily);
		return APIResult.successResult("编辑成功!");
	}

	 /**
	  * @description:  输入框查询是提示接口
	  * @param: hintVo
	  * @return:  模糊查询list
	  * @author liuzhonghua
	  * @date:
	  */
	 @RequestMapping("queryHint")
	 public APIResult<?> queryHint(@RequestBody FiBrDailyVO fiBrDailyVO) {
		 QueryWrapper<FiBrDaily> queryWrapper = new QueryWrapper<>();
		 if (!StringUtils.isEmpty(fiBrDailyVO.getFiBrDt())){
			 queryWrapper.eq("FI_BR_DT",fiBrDailyVO.getFiBrDt());
		 }
		 if (!StringUtils.isEmpty(fiBrDailyVO.getFiItemCd())){
			 queryWrapper.like("FI_ITEM_CD",fiBrDailyVO.getFiItemCd());
		 }else if (!StringUtils.isEmpty(fiBrDailyVO.getFiItemName())){
			 queryWrapper.like("FI_ITEM_NAME",fiBrDailyVO.getFiItemName());
		 }else {
		 	if ("1".equals(fiBrDailyVO.getFlag())){
				queryWrapper.select("DISTINCT FI_ITEM_CD");
			}else if ("2".equals(fiBrDailyVO.getFlag())){
				queryWrapper.select("DISTINCT FI_ITEM_NAME");
			}
		 }

		 List<FiBrDaily> list = fiBrDailyService.list(queryWrapper);
		 return APIResult.successResult(list);
	 }
	/**
	 *   通过id删除
	 */
	@OperationLog(value = "中小金融机构日报-通过id删除")
	@PostMapping(value = "/delete")
	public APIResult<?> delete(@RequestBody Map<String,String> map) {
		fiBrDailyService.removeById(map.get("id"));
		return APIResult.successResult("删除成功!");
	}

//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@OperationLog(value = "中小金融机构日报-批量删除")
//	@PostMapping(value = "/deleteBatch")
//	public APIResult<?> deleteBatch(@RequestBody List<FiBrDailyVO> ids) {
//		this.fiBrDailyService.removeByIds(ids.stream().map(FiBrDailyVO::getId).collect(Collectors.toList()));
//		return APIResult.successResult("批量删除成功!");
//	}

	/**
	 * 通过id查询
	 */
	@OperationLog(value = "中小金融机构日报-通过id查询")
	@PostMapping(value = "/queryById")
	public APIResult<?> queryById(@RequestParam(name="id") String id) {
		FiBrDaily fiBrDaily = fiBrDailyService.getById(id);
		if(fiBrDaily==null) {
			return APIResult.failResult(null, "未找到对应数据");
		}
		FiBrDailyVO fiBrDailyVO  = ConvertUtils.convert(FiBrDailyVO.class,fiBrDaily);
		return APIResult.successResult(fiBrDailyVO);
	}

    /**
    * 导出excel
    *
    */
    @RequestMapping(value = "/exportXls")
    public void exportXls(HttpServletResponse response, FiBrDailyVO fiBrDailyVO) {
		QueryWrapper<ExcelTemplate> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("TABLE_NAME","FI_BR_DAILY");
		ExcelTemplate template = excelTemplateService.getOne(queryWrapper);
		ExcelUtils.exportTemplate(template.getTemplateName(), template.getTemplateDir(), jdbcTemplate, response);
    }

    /**
      * 通过excel导入数据
    *
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public APIResult<?> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        return APIResult.successResult(super.importExcel(file, FiBrDaily.class));
    }
}
